package com.cgc.venus.server.user.service;

import com.cgc.venus.server.core.base.AbstractBaseService;
import com.cgc.venus.server.core.exception.VenusException;
import com.cgc.venus.server.core.utils.StringUtil;
import com.cgc.venus.server.user.bean.FunctionBean;
import com.cgc.venus.server.user.inter.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xyr on 2017/9/12.
 * 用户中心_用户管理_功能菜单模块
 */
@Service
public class FunctionServiceImpl extends AbstractBaseService implements FunctionService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /*
    * 是否存在相同的功能菜单
    * @param name 功能名
    * @param belongId 所属功能ID
    * */
    @Override
    public boolean exist(String name, String belongId) throws VenusException {
        String sql = "select id from t_function f where f.name=? and f.belong_id=? ";
        List<Object> list = new ArrayList<Object>();
        list.add(name);
        list.add(belongId);
        Map<String,Object> map = new HashMap<String, Object>();
        try{
            map = jdbcTemplate.queryForMap(sql,list.toArray());
        }catch (EmptyResultDataAccessException ex){
            return false;
        }
        if (map.size() == 0) return false;
        return true;
    }

    /*
    * 新增功能菜单
    * */
    @Override
    public int addFunction(FunctionBean functionBean) throws VenusException {
        String sql = "INSERT INTO t_function (id,name,is_menu,belong_id,url,active,create_time,last_update_time) " +
                "VALUES(?,?,?,?,?,?,NOW(),NOW())";
        List<Object> list = new ArrayList<Object>();
        list.add(StringUtil.getUUID());
        list.add(functionBean.getName());
        int isMenu = functionBean.getIsMenu() ? 1 : 0;
        list.add(isMenu);
        list.add(functionBean.getBelongId());
        list.add(functionBean.getUrl());
        int active = functionBean.getActive() ? 1 : 0;
        list.add(active);
        int row = jdbcTemplate.update(sql,list.toArray());
        return row;
    }

    /*
    * 修改功能菜单信息
    * */
    @Override
    public int updateFunction(FunctionBean functionBean) throws VenusException {
        String sql = "UPDATE t_function SET name=?, is_menu=?, belong_id=?,url=?,active=?,last_update_time=NOW() WHERE id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(functionBean.getName());
        int isMenu = functionBean.getIsMenu() ? 1 : 0;
        list.add(isMenu);
        list.add(functionBean.getBelongId());
        list.add(functionBean.getUrl());
        int active = functionBean.getActive() ? 1 : 0;
        list.add(active);
        list.add(functionBean.getId());
        int row = jdbcTemplate.update(sql,list.toArray());
        return row;
    }


    /*
    * 获得功能菜单树的根节点
    * */
    @Override
    public List<Map<String, Object>> getRootNode() throws VenusException {
        String sql = "SELECT id,name,is_menu AS isMenu,belong_id AS belongId, url, active FROM t_function WHERE belong_id = '' ";
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        try{
            mapList = jdbcTemplate.queryForList(sql);
        }catch (EmptyResultDataAccessException ex){
            return mapList;
        }
        return mapList;
    }

    /*
    * 获得功能菜单的子节点
    * */
    @Override
    public List<Map<String, Object>> getChildrenNode(String belongId) throws VenusException {
        // String sql = "SELECT id,name FROM t_function WHERE belong_id = ? ";
        String sql = "SELECT id,name,is_menu AS isMenu,belong_id AS belongId, url, active FROM t_function WHERE belong_id = ? ";
        List<Object> objects = new ArrayList<Object>();
        objects.add(belongId);
        List<Map<String,Object>> mapList = new ArrayList<Map<String, Object>>();
        try{
            mapList = jdbcTemplate.queryForList(sql,objects.toArray());
        }catch (EmptyResultDataAccessException ex){
            return mapList;
        }
        return mapList;
    }

    /*
    * 根据ID获得功能菜单的基本信息
    * */
    @Override
    public Map<String, Object> getFunctionById(String id) throws VenusException {
        String sql = "SELECT id,name,is_menu AS isMenu,belong_id AS belongId, url, active FROM t_function WHERE id= ? ";
        List<Object> objects = new ArrayList<Object>();
        objects.add(id);
        Map<String,Object> map = new HashMap<String, Object>();
        try {
            map = jdbcTemplate.queryForMap(sql,objects.toArray());
        } catch (EmptyResultDataAccessException ex) {
            return  map;
        }
        return map;
    }

    /*
    * 根据ID删除功能菜单信息
    * */
    @Override
    public int deleteFunction(String id) throws VenusException {
        String sql = "DELETE FROM t_function WHERE id=?";
        List<Object> list = new ArrayList<Object>();
        list.add(id);
        int row = jdbcTemplate.update(sql,list.toArray());
        return  row;
    }
}
